using System;
using System.Collections.Generic;
using System.Reflection;
namespace ServiceStack
{
	public static class TypeExtensions
	{
		public static Type[] GetReferencedTypes(this Type type)
		{
			HashSet<Type> hashSet = new HashSet<Type>
			{
				type
			};
			TypeExtensions.AddReferencedTypes(type, hashSet);
			return hashSet.ToArray<Type>();
		}
		public static void AddReferencedTypes(Type type, HashSet<Type> refTypes)
		{
			if (type.BaseType != null)
			{
				if (!refTypes.Contains(type.BaseType))
				{
					refTypes.Add(type.BaseType);
					TypeExtensions.AddReferencedTypes(type.BaseType, refTypes);
				}
				if (!type.BaseType.GetGenericArguments().IsEmpty<Type>())
				{
					Type[] array = type.BaseType.GetGenericArguments();
					for (int i = 0; i < array.Length; i++)
					{
						Type type2 = array[i];
						if (!refTypes.Contains(type2))
						{
							refTypes.Add(type2);
							TypeExtensions.AddReferencedTypes(type2, refTypes);
						}
					}
				}
			}
			PropertyInfo[] properties = type.GetProperties();
			if (!properties.IsEmpty<PropertyInfo>())
			{
				PropertyInfo[] array2 = properties;
				for (int i = 0; i < array2.Length; i++)
				{
					PropertyInfo propertyInfo = array2[i];
					if (!refTypes.Contains(propertyInfo.PropertyType))
					{
						refTypes.Add(propertyInfo.PropertyType);
						TypeExtensions.AddReferencedTypes(type, refTypes);
					}
					Type[] genericArguments = propertyInfo.PropertyType.GetGenericArguments();
					if (!genericArguments.IsEmpty<Type>())
					{
						Type[] array = genericArguments;
						for (int j = 0; j < array.Length; j++)
						{
							Type type3 = array[j];
							if (!refTypes.Contains(type3))
							{
								refTypes.Add(type3);
								TypeExtensions.AddReferencedTypes(type3, refTypes);
							}
						}
					}
					else
					{
						if (propertyInfo.PropertyType.IsArray)
						{
							Type elementType = propertyInfo.PropertyType.GetElementType();
							if (!refTypes.Contains(elementType))
							{
								refTypes.Add(elementType);
								TypeExtensions.AddReferencedTypes(elementType, refTypes);
							}
						}
					}
				}
			}
		}
	}
}
